import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams, } from "@angular/common/http";
import { NzModalService } from 'ng-zorro-antd/modal';
import { MainFormatService } from '../../core/services/main-format.service';
import { environment } from 'src/environments/environment';

@Injectable({
  providedIn: 'root'
})
export class PlotService {

  api_1226 = environment.api_1226

  constructor(
    private http: HttpClient,
    private mainFormatService: MainFormatService,
    private modal: NzModalService
  ) { }
  // 合并服务
  public getList(serviceID, params) {
    return this.http.get<any>(`mock-data/${serviceID}.json`, { params })
  }
  /**
   *
   * @param title 模态窗标题
   * @param component 组件
   * @param params   传入参数
   */
  openModel(title, component, params) {
    const modal = this.modal.create({
      nzTitle: title,
      nzContent: component,
      nzWidth: '60%',
      nzGetContainer: () => document.body, // 返回当前文档中的<body>元素或者<frameset>元素
      nzComponentParams: {
        Url: params
      },
      nzOnOk: () => { },
      nzFooter: null
    });
    modal.afterOpen.subscribe(() => console.log('[afterOpen] emitted!'));
    // Return a result when closed
    modal.afterClose.subscribe(result => console.log('[afterClose] The result is:', result));
  }

  /**
   * 根据逗号分隔字符串
   *  @param element string
   *  返回 Array
   */
  getObjPath(element) {
    const pdfArr = [];
    let pathArrStr = null;
    if (element.indexOf(',') > -1) {
      pathArrStr = element.split(',');
    } else {
      pathArrStr = [element];
    }
    pathArrStr.forEach(element1 => {
      const names = (element1.split('/')[element1.split('/').length - 1]);
      pdfArr.push({ name: names, path: element1 });
    });
    return pdfArr;
  }
  dataFormat(data) {
    if (!Array.isArray(data)) {
      return;
    }
    data.forEach(element => {
      if (element.bgcflj && element.bgcflj.length > 1) {
        element.bgcflj = this.getObjPath(element.bgcflj);
      }
      if (element.spcflj) {
        element.spcflj = this.getObjPath(element.spcflj);
      }
    });
    return data;
  }

  /**
   * 所有服务同意调用口
   * @param serviceID  服务/接口ID
   * @param param 参数
   * @param forArr 字段转换数组
   * @param collback 回调函数 -- 返回处理好的数据
   */

  backServiceData(serviceID, params, forArr, collback) {
    this.getList(serviceID, params).subscribe(res => {
      if (res.code === 1 && res.data) {
        const list = this.mainFormatService.fieldAdaptive(res.data.list, forArr);
        const rlist = this.dataFormat(list);
        const totalRecord = res.data.totalRecord;
        collback(rlist, totalRecord);
      } else {
        collback(null, 0);
      }
    });
  }

  // 决策计算
  decisionCalc(params) {
    const x = {
      aa: 18,
      ab: 2.41,
      ac: 0.93,
      ad: 0.44,
      ae: 1.2,
      af: 0.19,
      ag: 0.06,
      ah: 0.03,
      ai: 0.069,
      aj: 0.021,
      ak: 0.009,
      al: 0.004,
      am: 1,
      an: 2,
      ao: 3,
      ap: 4,
      aq: 4,
      ar: 3,
      as: 2,
      at: 1,
      au: 1,
      av: 2,
      aw: 3,
      ax: 4,
      ay: 4,
      az: 3,
      ba: 2,
      bb: 1,
      bc: 1,
      bd: 2,
      be: 3,
      bf: 4,
      bg: 4,
      bh: 3,
      bi: 2,
      bj: 1,
      a: 'B'
      , a1: 3,
      b: 'B'
      , b1: 5,
      c: 'B'
      , c1: 9,
      d: 'A'
      , d1: 3,
      e: 'B'
      , e1: 3,
      f: 'B'
      , f1: 5,
      g: 'A'
      , g1: 7,
      h: 'A'
      , h1: 5,
      i: 'B'
      , i1: 3,
      j: 'A'
      , j1: 5,
      k: 'A'
      , k1: 3,
      l: 'A'
      , l1: 5,
      m: 'A'
      , m1: 3
    }
    return this.http.post(environment.calc_url, params);
  }

  //污染数据-地下水
  getLandWater(params) {
    return this.http.get<any>(`${this.api_1226}/land/water/poll/`, {
      params
    })
  }
  //污染数据-土壤
  getLandSolid(params) {
    return this.http.get<any>(`${this.api_1226}/land/soil/poll/`, {
      params
    })
  }
}
